// 生成路由地址数组 与 路由映射表
export default function createRouteMap (routes, pathList, pathMap) {
  pathList = pathList || []
  // 键为路由地址 值为路由信息
  pathMap = pathMap || {} // {'/music': {...}, ...}

  routes.forEach(route => {
    addRouteRecord(route, pathList, pathMap)
  })

  return {
    pathList,
    pathMap
  }
}
// 动态添加路由
function addRouteRecord (route, pathList, pathMap, parentRecord) {
  // 存在父组件 则拼接路径
  const path = parentRecord ? `${parentRecord.path}/${route.path}` : route.path
  // 路由的相关信息
  const record = {
    path: path,
    component: route.component,
    // 记录父组件
    parentRecord: parentRecord
  }
  // 防止重复添加
  if (!pathMap[path]) {
    pathList.push(path)
    pathMap[path] = record
  }

  // 递归处理子路由
  if (route.children) {
    route.children.forEach(childRoute => {
      addRouteRecord(childRoute, pathList, pathMap, record)
    })
  }
}
